home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5757 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  70 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Nested Template Classes
  5. Date: 06 Feb 1996 17:02:58 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Feb6180259@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4f6kql$70s@charnel.ecst.csuchico.edu>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: mcelroy@ecst.csuchico.edu's message of 6 Feb 1996 04:18:29 GMT
  12.  
  13. In article <4f6kql$70s@charnel.ecst.csuchico.edu> mcelroy@ecst.csuchico.edu (James Robert McElroy) writes:
  14.  
  15.    Here's a real brain-twister for you, something hiding
  16.    off in the dark recesses of C++dom.
  17.  
  18.    What does the function definition for a nested template
  19.    class function look like?  IS there such a construct?  I have
  20.    not been able to find one that works (except for inlining
  21.    every nested template class function).
  22.  
  23.    e.g.   Normal:
  24.  
  25.    class list {
  26.        private:
  27.       class node {
  28.          private:
  29.         int data;
  30.         node * next;
  31.          public:
  32.         node(int someData);
  33.         int getData();
  34.         etc...
  35.        };
  36.        public:
  37.      whatever...
  38.  
  39.    };
  40.  
  41.    int list::node::getData() { return data; }  // okay -- correct form
  42.  
  43.  
  44.    Templated:  take the above class definition and templatize
  45.            it, replacing the type of node::data with the
  46.            template parameter "T" instead of "int".
  47.  
  48.            Now, assuming you don't want to inline the 
  49.            getData method, how do you define it?
  50.  
  51.    template <class T>
  52.    T list<T>::node<T>::getData() { return data; }  // doesn't work
  53.  
  54.    template <class T>
  55.    T list<T>::node::getData() { return data; }     // doesn't work
  56.  
  57.    template <class T>
  58.    T list::node<T>::getData() { return data; }     // doesn't work
  59.  
  60.    template <class T>
  61.    T list::node::getData() { return data; }        // doesn't work, 
  62.                               of course.
  63.  
  64.  
  65.    The compiler barfs on all of these.      
  66.  
  67. The 2nd solution is correct.
  68.  
  69.     Enno
  70.